package me.ywd.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Ye_Wenda
 * @Date 7/31/2017
 */
@RestController
public class ProviderDemoController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("services")
    public List<String> clientList(){
        List<String> serviceIds = discoveryClient.getServices();
        for(String serviceId : serviceIds) {
            List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
            for (ServiceInstance instance : instances) {
                System.out.println("ServiceId:" + instance.getServiceId() + ",Host:" + instance.getHost() + ",Port:" + instance.getPort() + ",URI:" + instance.getUri() + ",MetaData:" + instance.getMetadata());
            }
        }
        return serviceIds;
    }

    @RequestMapping("/test")
    public String test() {
        return null;
    }
}
